<?php
class common extends admin{
    
    public function __construct(){
        parent::__construct();
    }
    
    public function index(){}
    
    //获取后台菜单
    public function getmenu(){
        $where=' where is_menu=1 ';
        if($_SESSION['client']['parentid']!=0) {
            $where.=' and id in ('.$_SESSION['client']['auth_node'].')';
        }
        $list=db()->getAll('select * from x_jiaxiao_menu '.$where);
        if(!$list) $list=array();
        $this->ajaxReturn(array('status'=>1,'data'=>$list));
    }
    
    //获取当前管理员管理的驾校列表
    public function get_school(){
        if(empty($_SESSION['client']['school_id'])) $this->ajaxReturn(array('status'=>0,'info'=>''));
        $list=db()->getAll('select id,name from x_jiaxiaoinfo where id in('.$_SESSION['client']['school_id'].')');
        if($list){
            $current=$list[0];
            if(count($list)>1){
                foreach($list as $k=>$v){
                    if($v['id']==$_SESSION['client']['jx_id']) {
                        $current=$v;
                        break;
                    }
                }
            }
            $this->ajaxReturn(array('status'=>1,'data'=>$list,'current'=>$current));
        }else{
            $this->ajaxReturn(array('status'=>0,'info'=>'获取驾校数据失败'));
        }
    }
    
    //切换驾校
     public function change_school(){
        $id=(int)$_POST['id'];
        if($id<=0) $this->ajaxReturn(array('status'=>0,'info'=>'驾校id不正确'));
        if(strpos(','.$_SESSION['client']['school_id'].',',','.$id.',')===false){
            $this->ajaxReturn(array('status'=>0,'info'=>'您没有该驾校管理权，请联系管理员'));
        }else{
            $_SESSION['client']['jx_id']=$id;//当前客户端的驾校id
            $this->ajaxReturn(array('status'=>1));//切换成功
        }
    }
    
    //退出登录
    public function logout(){
        unset($_SESSION['client']);
        $this->ajaxReturn(array('status'=>1));
    }
    
    //上传文件
    public function upload(){
        $type=isset($_POST['type'])?$_POST['type']:'common';
        $sub=isset($_POST['sub'])?$_POST['sub']:date('Ymd');
        $upload_path = C('UPLOAD_PATH').'/'.$type.'/'.$sub;//上传目录
        
        if(!is_dir($upload_path)) mkdir($upload_path,0700,true);
        
        $result=array();
        
        foreach ($_FILES as $k=> $v){
            $info=pathinfo($v['name']);
            $file_path=$upload_path.'/'.time().rand(1,1000).'.'.$info['extension'];
            if(move_uploaded_file($v["tmp_name"],$file_path)){
                $result[$k]=array('path'=>'http://'.$_SERVER['HTTP_HOST'].'/'.$file_path,'status'=>true);
            }else{
                $result[$k]=array('path'=>$file_path,'status'=>false);
            }
        }
        
        $this->ajaxReturn(array('status'=>1,'data'=>$result));
    }
    
    
}